//
//  ZYCProtalCalendarStatisticsViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/21.
//

#import "ZYCProtalCalendarStatisticsViewController.h"
#import "ZYCProtalCalendarStatisticsCell.h"
#import "ZYCMenuSliderView.h"
#import "ZYCCalendarStatisticsModel.h"

@interface ZYCProtalCalendarStatisticsViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCMenuSliderViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) ZYCMenuSliderView *menuSliderView;
@property (nonatomic, assign) NSInteger checkCount;
@property (nonatomic, assign) NSInteger unCheckCount;

@property (nonatomic, assign) BOOL isRead;
@end

@implementation ZYCProtalCalendarStatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.titleString;
    self.isRead = YES;
    [self.view addSubview:self.tableView];
    [self loadData];
}

// 已读/ 未读列表
- (void)loadData{
    NSDictionary *params = @{
        @"noticeId":self.noticeId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_QueryReadList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *checkList = [NSArray yy_modelArrayWithClass:[ZYCCalendarStatisticsModel class] json:responseObject[@"body"][@"checkList"]];
                self.checkCount = checkList.count;
                NSArray *uncheckList = [NSArray yy_modelArrayWithClass:[ZYCCalendarStatisticsModel class] json:responseObject[@"body"][@"uncheckList"]];
                self.unCheckCount = uncheckList.count;
                
                self.tableView.tableHeaderView = self.menuSliderView;
                if (self.isRead) {
                    [self.dataSource removeAllObjects];
                    [self.dataSource addObjectsFromArray:checkList];
                }else{
                    [self.dataSource removeAllObjects];
                    [self.dataSource addObjectsFromArray:uncheckList];
                }
                [self.tableView reloadData];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

- (ZYCMenuSliderView *)menuSliderView {
    if (!_menuSliderView) {
        _menuSliderView = [[ZYCMenuSliderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 46.0f)titles:@[@{@"title":@"已读",@"count":@(self.checkCount)},@{@"title":@"未读",@"count":@(self.unCheckCount)}]];
        _menuSliderView.delegate = self;
    }
    return _menuSliderView;
}

#pragma mark - ZYCMenuSliderViewDelegate
- (void)menuSilderClickRight{
    self.isRead = NO;
    [self loadData];
}

- (void)menuSilderClickLeft{
    self.isRead = YES;
    [self loadData];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCProtalCalendarStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCProtalCalendarStatisticsCell class]) forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.section];
    cell.isRead = self.isRead;
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCProtalCalendarStatisticsCell class] forCellReuseIdentifier:NSStringFromClass([ZYCProtalCalendarStatisticsCell class])];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
